package cn.wh.org.controller;

import cn.wh.basic.anno.PreAuthorize;
import cn.wh.basic.util.AjaxResult;
import cn.wh.basic.util.PageList;
import cn.wh.org.domain.Department;
import cn.wh.org.query.DepartmentQuery;
import cn.wh.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")

public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;


    @GetMapping("/{id}")
    @PreAuthorize(name="查找一条部门数据", value = "dept:queryById")
    public Department queryById(@PathVariable Long id){
        return departmentService.queryById(id);
    }

    @GetMapping
    @PreAuthorize(name="查找全部数据", value = "dept:queryAll")
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    @PutMapping
    @PreAuthorize(name="新增修改部门数据", value = "dept:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.save(department);
            }else{
                departmentService.update(department);
            }
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(true).setMessage("弄给和hiGV橘郡");

        }

    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name="删除一条部门数据", value = "dept:deleteById")
    public AjaxResult removeById(@PathVariable Long id){
        try {
            departmentService.removeById(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(true).setMessage("弄给和hiGV橘郡");
        }
    }

    @DeleteMapping
    @PreAuthorize(name="删除所有部门数据", value = "dept:removeAll")
    public AjaxResult removeAll(@RequestBody List<Long> ids){
        try {
            departmentService.deleteAll(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(true).setMessage("弄给和hiGV橘郡");
        }
    }


    @GetMapping("/tree")
    public List<Department> findTree(){

        return departmentService.queryTree();
    }

}
